package com.vatsalya.bakthi.yoga;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ToggleButton;

import com.vatsalya.bakthi.yoga.broadcastreceivers.TimeAlarm;

public class AlarmSettingsActivity extends Activity {

	AlarmManager alarmManager;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alarmsettings);
		alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
	}

	public void onSaveClicked(View view) {
		ToggleButton rahuKaalam = (ToggleButton) findViewById(R.id.rahukaalamToggleButton);
		ToggleButton yemakandam = (ToggleButton) findViewById(R.id.yemakandamToggleButton);
		
		clearAllAlarms();
		if (rahuKaalam.isChecked()) {
			setRahuKaalamAlarms();
		}

		if (yemakandam.isChecked()) {
			setYemaKandamAlarms();
		}

	}

	private void setRahuKaalamAlarms() {
		setRaahuKaalam(1, 16, 30,"4.30 pm to 6.00 pm");
		setRaahuKaalam(2, 7, 30,"7.30 am to 9.00 am");
		setRaahuKaalam(3, 15, 00,"3.00 pm to 4.30 pm");
		setRaahuKaalam(4, 12, 00,"12.00 pm to 1.30 pm");
		setRaahuKaalam(5, 13, 30,"1.30 pm to 3.00 pm");
		setRaahuKaalam(6, 10, 30,"10.30 am to 12.00 pm");
		setRaahuKaalam(7, 9, 00,"9.00 am to 10.30 pm");
	}

	private void setYemaKandamAlarms() {
		setYemaKandam(1, 12, 00,"12.00 pm to 1.30 pm");
		setYemaKandam(2, 10, 30,"10.30 am to 12.00 pm");
		setYemaKandam(3, 9, 00,"9.00 am to 10.30 pm");
		setYemaKandam(4, 7, 30,"7.30 am to 9.00 am");
		setYemaKandam(5, 6, 00,"6.00 am to 7.30 am");
		setYemaKandam(6, 15, 00,"3.00 pm to 4.30 pm");
		setYemaKandam(7, 13, 30,"1.30 pm to 3.00 pm");
	}
	
	
	private void setRaahuKaalam(int dayofWeek, int hour, int minute, String message) {
		// Set for sunday
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK, dayofWeek);
		calendar.set(Calendar.HOUR, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
	
		Intent intent = new Intent(this, TimeAlarm.class);
		intent.putExtra("message", String.format("Raahu Kaalam %s", message));
		PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
				dayofWeek + 7, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
				calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7,
				pendingIntent);
	}

	private void setYemaKandam(int dayofWeek, int hour, int minute, String message) {
		// Set for sunday
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK, dayofWeek);
		calendar.set(Calendar.HOUR, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);

		Intent intent = new Intent(this, TimeAlarm.class);
		intent.putExtra("message",  String.format("Yema Kandam %s", message));
		PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
				dayofWeek, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
				calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7,
				pendingIntent);
		
	}

	private void clearAllAlarms() {

		for (int i = 1; i <= 14; i++) {
			Intent intent = new Intent(this, TimeAlarm.class);
			PendingIntent pendingIntent = PendingIntent.getBroadcast(this, i,
					intent, PendingIntent.FLAG_CANCEL_CURRENT);
			alarmManager.cancel(pendingIntent);

		}
	}

}
